package com.hy.common;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Description:
 * @Author: wdk
 * @CreateTime: 2024/9/7 18:40
 */
public class EndMoreTextView extends androidx.appcompat.widget.AppCompatTextView {
    private static final String TAG = "EndMoreTextView";

    public EndMoreTextView(@NonNull Context context) {
        super(context);
    }

    public EndMoreTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public EndMoreTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
    }

    private String moreText = "…更多";

    public void setMoreText(String moreText) {
        this.moreText = "…" + moreText;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();

        Log.e(TAG, "onMeasure: viewWidth=" + viewWidth + "  viewHeight=" + viewHeight);

        float textSize = getTextSize();
        String text = getText().toString();
        int textLength = text.length();
        float allTextWidth = textSize * textLength;
        Log.e(TAG, "onMeasure: textSize=" + textSize + "  allTextWidth=" + allTextWidth);
        Log.e(TAG, "onMeasure: text=" + text+ "  textLength="+textLength);

        //需要拼接更多
        if (allTextWidth > viewWidth) {
            float moreTextLength = moreText.length() * textSize;
            Log.e(TAG, "onMeasure: moreTextLength=" + moreTextLength);
            int textWordCount = (int) ((viewWidth - moreTextLength) / textSize);
            Log.e(TAG, "onMeasure: textWordCount=" + textWordCount);
            text = text.substring(0, textWordCount);
            String lastText = text + moreText;

            final SpannableStringBuilder style = new SpannableStringBuilder();

            //设置文字
            style.append(lastText);

            //设置部分文字点击事件
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(getContext(), "触发点击事件!", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    ds.setColor(Color.RED);
                    ds.setUnderlineText(false);
                    ds.bgColor=Color.WHITE;
                }
            };
            style.setSpan(clickableSpan, text.length(), lastText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            setText(style);

            //配置给TextView
            setMovementMethod(LinkMovementMethod.getInstance());
            setText(style);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


    }
}
